#include "DoubleLinkList.h"
#include "LinkTree.h"
#include <stdio.h>
#include <MyString.h>
#include <dirent.h>
#define GetNode(str) FindLinkTreeNode(tree,(void*)str,IsNameEqual)

void Print(void *ptr)
{
    printf("%s\n",(char*)ptr);
}
bool IsNameContains(void *ptr1, void *ptr2)
{
    INSERTSTR(str,(char*)ptr1);
    IsContains(&str,(char*)ptr2);
}

bool IsNameEqual(void *ptr1, void *ptr2)
{
    INSERTSTR(str,(char*)ptr1);
    return IsEqual(&str,(char*)ptr2);
}

TNode *FindNodeByPath(LTree *tree, const char *path)
{
    MyStrList strlist = Split(path,"/");
    TNode *target = GetTreeRoot(tree);
    for(int i = 0; i < strlist.len; i++)
    {
        target = FindTreeNode(target,(void*)strlist.stringList[i].string,IsNameEqual);
        if(target == NULL)
        {
            printf("没找到！\n");
            return NULL;
        }
    }
    return target;
}

// int main()
// {
//     LTree *tree = InitLinkTree();

//     TNode *class1 = CreateTreeNode("251");
//     TNode *class2 = CreateTreeNode("252");
//     ConnectBranch(GetTreeRoot(tree),class1);
//     ConnectBranch(GetTreeRoot(tree),class2);


//     ConnectBranch(GetNode("251"),CreateTreeNode("第一组"));
//     ConnectBranch(GetNode("251"),CreateTreeNode("第二组"));
//     ConnectBranch(GetNode("252"),CreateTreeNode("第一组"));
//     ConnectBranch(GetNode("252"),CreateTreeNode("第二组"));

//     ConnectBranch(FindNodeByPath(tree,"251/第一组"),CreateTreeNode("张三"));
//     ConnectBranch(FindNodeByPath(tree,"251/第二组"),CreateTreeNode("李四"));

//     ConnectBranch(FindNodeByPath(tree,"252/第一组"),CreateTreeNode("王五"));
//     ConnectBranch(FindNodeByPath(tree,"252/第二组"),CreateTreeNode("六子"));
    

//     TravelTree(tree,Print);
    
//     DLlist leaves = FindAllLeavesBranches(tree);
//     DLNode *travelPoint = leaves.head->next;
//     while(travelPoint->next != NULL)
//     {
//         TNode *n = (TNode *)travelPoint->next->data;
//         Print(GetTreeNodeData(n));
//         travelPoint = travelPoint->next;
//     }

//     ClearLinkTree(tree);
//     return 0;
// }

void ExploreDirectory(const char *path, TNode *node)
{
    //获取文件夹指针
    DIR *d = opendir(path);
    if(d == NULL)
        return;

    struct dirent *e = NULL;
    while((e = readdir(d)) != NULL)
    {
        if(strcmp(e->d_name,".") == 0 || strcmp(e->d_name,"..") == 0)
        {
            continue;
        }
    
        //根据文件名创建一个新的节点
        TNode *fileNode = CreateTreeNode(GetString(e->d_name).string);
        ConnectBranch(node,fileNode);

        //如果是一个文件夹
        if(e->d_type == 4)
        {
            char filePath[1024] = {0};
            strcat(filePath,path);
            strcat(filePath,"/");
            strcat(filePath,e->d_name);
            ExploreDirectory(filePath,fileNode);
        }
    }
    closedir(d);
}


int main(int argc, char *argv[])
{
    if(argc != 1 && argc != 2)
    {
        printf("invalid values!\n");
        return -1;
    }

    char *path = argv[1];
    if(argc == 1)
    {
        path = "./";
    }
    else
    {
        path = argv[1];
    }

    LTree *t = InitLinkTree();

    TNode *pathNode = CreateTreeNode(path);
    ConnectBranch(GetTreeRoot(t),pathNode);

    ExploreDirectory(path,pathNode);

    TravelTree(t,Print);

    ClearLinkTree(t);
    return 0;
}